<?php

/**
 * 运单打印
 * * */

namespace app\seller\controller;

class Waybill extends \app\common\controller\Seller
{

    /**
     * 模板管理
     */
    public function waybill_manage()
    {
        $model_store_extend = model('Common/StoreExtend');
        $model_express = model('Common/Express');
        $model_store_waybill = model('Common/StoreWaybill');

        $store_express = $model_store_extend->getStoreExtendInfo(array('store_id' => $this->store_id), 'express');

        $express_list = $model_express->getExpressListByID($store_express);
        $store_express = array_column($express_list, 'id');

        $store_waybill_list = $model_store_waybill->getStoreWaybillListWithWaybillInfo($this->store_id, $store_express);
        $store_waybill_list = reset_by_key($store_waybill_list, 'express_id');

        if (!empty($express_list)) {
            foreach ($express_list as $key => $value) {
                if (!empty($store_waybill_list[$value['id']])) {
                    $express_list[$key]['waybill_name'] = $store_waybill_list[$value['id']]['waybill_name'];
                    $express_list[$key]['store_waybill_id'] = $store_waybill_list[$value['id']]['store_waybill_id'];
                    $express_list[$key]['is_default_text'] = $store_waybill_list[$value['id']]['is_default'] ? '是' : '否';
                    $express_list[$key]['waybill_image_url'] = img_url($store_waybill_list[$value['id']]['waybill_image']);
                    $express_list[$key]['waybill_width'] = $store_waybill_list[$value['id']]['waybill_width'];
                    $express_list[$key]['waybill_height'] = $store_waybill_list[$value['id']]['waybill_height'];
                    $express_list[$key]['bind'] = true;
                } else {
                    $express_list[$key]['waybill_name'] = '未绑定';
                    $express_list[$key]['bind'] = false;
                }
            }
        }

        $this->assign('express_list', $express_list);

        $this->profile_menu('waybill_manage');
        return $this->fetch();
    }

    /**
     * 绑定运单打印模板
     */
    public function waybill_bind()
    {
        $express_id = input('express_id/d');

        $model_express = model('Common/Express');
        $model_waybill = model('Common/Waybill');

        $express_info = $model_express->getExpressInfo($express_id);
        if (empty($express_info)) {
            return showDialog('快递公司不存在');
        }
        $this->assign('express_info', $express_info);

        $waybill_list = $model_waybill->getWaybillUsableList($express_id, $this->store_id);
        $this->assign('waybill_list', $waybill_list);


        $this->profile_menu('waybill_bind');
        return $this->fetch();
    }

    /**
     * 绑定运单打印模板保存
     */
    public function waybill_bind_save()
    {
        $express_id = input('express_id/d');

        $model_waybill = model('Common/Waybill');
        $model_store_waybill = model('Common/StoreWaybill');

        $waybill_info = $model_waybill->getWaybillInfoByID(input('waybill_id/d'));
        if (!$waybill_info) {
            return $this->error('运单模板不存在');
        }

        $param = array();
        $param['store_id'] = $this->store_id;
        $param['express_id'] = $express_id;

        //删除已有绑定
        $model_store_waybill->delStoreWaybill($param);

        //保存绑定
        $param['waybill_id'] = $waybill_info['waybill_id'];
        $param['waybill_name'] = $waybill_info['waybill_name'];
        $param['store_waybill_left'] = $waybill_info['waybill_left'];
        $param['store_waybill_top'] = $waybill_info['waybill_top'];
        $result = $model_store_waybill->addStoreWaybill($param);
        if ($result) {
            return $this->success('绑定成功', url('seller/waybill/waybill_manage'), '', 1);
        } else {
            return $this->error('绑定失败', '', '', 'error');
        }
    }

    /**
     * 解绑运单打印模板
     */
    public function waybill_unbind()
    {
        $store_waybill_id = input('store_waybill_id/d');

        $model_store_waybill = model('Common/StoreWaybill');

        $condition = array();
        $condition['store_waybill_id'] = $store_waybill_id;
        $condition['store_id'] = $this->store_id;

        $result = $model_store_waybill->delStoreWaybill($condition);
        if ($result) {
            return $this->success('解绑成功', null, '', 1);
        } else {
            return $this->error('解绑失败');
        }
    }

    /**
     * 运单模板设置
     */
    public function waybill_setting()
    {
        $store_waybill_id = input('store_waybill_id/d');

        $model_store_waybill = model('Common/StoreWaybill');

        $store_waybill_info = $model_store_waybill->getStoreWaybillInfo(array('store_waybill_id' => $store_waybill_id));
        $this->assign('store_waybill_id', $store_waybill_info['store_waybill_id']);
        $this->assign('store_waybill_left', $store_waybill_info['store_waybill_left']);
        $this->assign('store_waybill_top', $store_waybill_info['store_waybill_top']);
        $this->assign('store_waybill_data', $store_waybill_info['store_waybill_data']);

        $this->profile_menu('waybill_setting');
        return $this->fetch();
    }

    /**
     * 运单模板设置保存 
     */
    public function waybill_setting_save()
    {
        $store_waybill_id = input('post.store_waybill_id/d');
        if ($store_waybill_id <= 0) {
            return $this->error('参数错误');
        }

        $model_store_waybill = model('Common/StoreWaybill');

        $condition = array();
        $condition['store_waybill_id'] = $store_waybill_id;
        $condition['store_id'] = $this->store_id;

        $update = array();
        $update['store_waybill_left'] = input('post.store_waybill_left');
        $update['store_waybill_top']  = input('post.store_waybill_top');

        $result = $model_store_waybill->editStoreWaybill($update, $condition, $_POST['data']);
        if ($result) {
            return showDialog('保存成功', url('seller/waybill/waybill_manage'), 'succ');
        } else {
            return showDialog('保存失败', '', 'error');
        }
    }

    /**
     * 运单打印测试
     */
    public function waybill_test()
    {
        $model_waybill = model('Common/Waybill');

        $waybill_info = $model_waybill->getWaybillInfoByID(input('waybill_id/d'));
        if (!$waybill_info) {
            return $this->error('运单模板不存在');
        }

        $this->assign('waybill_info', $waybill_info);
        return $this->fetch();
    }

    /**
     * 设置默认打印模板
     */
    public function waybill_set_default()
    {
        $store_waybill_id = intval($_POST['store_waybill_id']);

        $model_store_waybill = model('Common/StoreWaybill');

        $result = $model_store_waybill->editStoreWaybillDefault($store_waybill_id, $this->store_id);

        if ($result) {
            return $this->success('保存成功');
        } else {
            return $this->error('保存失败');
        }
    }

    /**
     * 模板列表
     */
    public function waybill_list()
    {
        $model_waybill = model('Common/Waybill');

        $waybill_list = $model_waybill->getWaybillSellerList($this->store_id);
        $this->assign('waybill_list', $waybill_list);

        $this->profile_menu('waybill_list');
        return $this->fetch();
    }

    /**
     * 添加运单模板
     */
    public function waybill_add()
    {
        $model_express = model('Common/Express');

        $this->assign('express_list', $model_express->getExpressList());
        $this->profile_menu('waybill_add');
        return $this->fetch();
    }

    /**
     * 保存运单模板
     */
    public function waybill_save()
    {
        $model_waybill = model('Common/Waybill');
        $post = input('post.');
        
        $param = array();
        $param['waybill_name'] = $post['waybill_name'];
        $param['waybill_width'] = $post['waybill_width'];
        $param['waybill_height'] = $post['waybill_height'];
        $param['waybill_left'] = $post['waybill_left'];
        $param['waybill_top'] = $post['waybill_top'];
        $param['waybill_usable'] = $post['waybill_usable'];
        $param['store_id'] = $this->store_id;
        list($param['express_id'], $param['express_name']) = explode('|', $post['waybill_express']);

        //图片上传
        if ($this->request->file('waybill_image')) {
            $waybill_image = $this->uploadFile('waybill_image', 'waybill', false, ['ext' => 'jpg,png,jpeg']);
            if ($waybill_image['status']) {
                $param['waybill_image'] = $waybill_image['save_path_type'].$waybill_image['save_name'];
                if (!empty($param['old_waybill_image'])) {
                    $model_waybill->delWaybillImage($post['old_waybill_image']);
                }
            } else {
                return $this->error('图片上传失败：'.$waybill_image['error']);
            }
        }

        //验证数据
        $obj_validate = new \think\Validate([
            'waybill_name|模板名称' => 'require',
            'waybill_width|宽度' => 'require',
            'waybill_name|高度' => 'require',
        ]);
        
        if (!$obj_validate->check($param)) {
            return array('error' => $obj_validate->getError());
        }

        if (empty($post['waybill_id'])) {
            //添加
            $result = $model_waybill->addWaybill($param);
            $waybill_id = $result;
        } else {
            //编辑 
            $condition = array();
            $condition['waybill_id'] = intval($post['waybill_id']);
            if ($store_id > 0) {
                $condition['store_id'] = $store_id;
            }
            $result = $model_waybill->editWaybill($param, $condition);
            $waybill_id = $post['waybill_id'];
        }

        if ($waybill_id) {
            return showDialog('保存成功', url('seller/waybill/waybill_list'), 'succ');
        } else {
            return showDialog('保存失败', url('seller/waybill/waybill_list'));
        }
    }

    /**
     * 删除运单模板
     */
    public function waybill_del()
    {
        $waybill_id = input('post.waybill_id/d');
        if ($waybill_id < 1) {
            return $this->error('参数错误');
        }

        $model_waybill = model('Common/Waybill');

        $condition = array();
        $condition['waybill_id'] = $waybill_id;
        $condition['store_id'] = $this->store_id;
        $result = $model_waybill->delWaybill($condition);
        if ($result) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }

    /**
     * 编辑运单模板
     */
    public function waybill_edit()
    {
        $model_express = model('Common/Express');
        $model_waybill = model('Common/Waybill');

        $waybill_id = input('waybill_id/d');
        $waybill_info = $model_waybill->getWaybillInfoByID($waybill_id);
        if (!$waybill_info || $waybill_info['store_id'] != $this->store_id) {
            return $this->error('运单模板不存在');
        }
        $this->assign('waybill_info', $waybill_info);

        $express_list = $model_express->getExpressList();
        foreach ($express_list as $key => $value) {
            if ($value['id'] == $waybill_info['express_id']) {
                $express_list[$key]['selected'] = true;
            }
        }
        $this->assign('express_list', $express_list);

        $this->profile_menu('waybill_edit');
        return $this->fetch('waybill_add');
    }

    /**
     * 设计运单模板
     */
    public function waybill_design()
    {
        $model_waybill = model('Common/Waybill');

        $result = $model_waybill->getWaybillDesignInfo(input('waybill_id/d'));
        if (isset($result['error'])) {
            return $this->error($result['error']);
        }

        $this->assign('waybill_info', $result['waybill_info']);
        $this->assign('waybill_info_data', $result['waybill_info_data']);
        $this->assign('waybill_item_list', $result['waybill_item_list']);

        $this->profile_menu('waybill_design');
        return $this->fetch();
    }

    /**
     * 设计运单模板保存
     */
    public function waybill_design_save()
    {
        $model_waybill = model('Common/Waybill');

        $data = input('post.');
        $result = $model_waybill->editWaybillDataByID($data['waybill_data'], $data['waybill_id'], $this->store_id);

        if ($result) {
            return $this->success('保存成功', url('seller/waybill/waybill_list'));
        } else {
            return $this->error('保存失败');
        }
    }

    /**
     * 页面菜单
     *
     * @param string 	$menu_key	当前导航的menu_key
     * @return
     */
    private function profile_menu($menu_key = '')
    {
        $menu_array = array();
        $menu_array[] = array(
            'menu_key' => 'waybill_manage',
            'menu_name' => '模板绑定',
            'menu_url' => url('seller/waybill/waybill_manage')
        );
        $menu_array[] = array(
            'menu_key' => 'waybill_list',
            'menu_name' => '自建模板',
            'menu_url' => url('seller/waybill/waybill_list')
        );
        if ($menu_key == 'waybill_bind') {
            $menu_array[] = array(
                'menu_key' => 'waybill_bind',
                'menu_name' => '选择模板',
                'menu_url' => url('seller/waybill/waybill_bind')
            );
        }
        if ($menu_key == 'waybill_setting') {
            $menu_array[] = array(
                'menu_key' => 'waybill_setting',
                'menu_name' => '模板设置',
                'menu_url' => url('seller/waybill/waybill_setting')
            );
        }
        if ($menu_key == 'waybill_add') {
            $menu_array[] = array(
                'menu_key' => 'waybill_add',
                'menu_name' => '添加模板',
                'menu_url' => url('seller/waybill/waybill_add')
            );
        }
        if ($menu_key == 'waybill_edit') {
            $menu_array[] = array(
                'menu_key' => 'waybill_edit',
                'menu_name' => '编辑模板',
                'menu_url' => url('seller/waybill/waybill_edit')
            );
        }
        if ($menu_key == 'waybill_design') {
            $menu_array[] = array(
                'menu_key' => 'waybill_design',
                'menu_name' => '设计模板',
                'menu_url' => url('seller/waybill/waybill_design')
            );
        }
        $this->assign('member_menu', $menu_array);
        $this->assign('menu_key', $menu_key);
        $this->assign('left_nav', 'waybill');
    }

}
